package de.lighti.starcraft.model;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

public class StarcraftBot implements Serializable, Cloneable {
    /**
     * 
     */
    private static final long serialVersionUID = -6489080855382438747L;
    /** The name of the Bot */
    private final String name;
    /** The random Hex value choosen by the design team */
    private final Long randomHex;
    /**
     * The race the bot is playing from the BWAPI settings Terran, Protoss,
     * Zerg, Random RandomTP, RandomTZ or RandomPZ
     */
    private final String race;

    private final boolean isBenchmark;

    private transient Map<String, ByteBuffer> files;

    protected StarcraftBot( StarcraftBot other ) {
        this( other.name, other.randomHex, other.race, other.isBenchmark );
    }

    public StarcraftBot( String name, Long hex, String race, boolean isBenchmark ) {
        super();

        this.name = name;
        randomHex = hex;
        this.race = race;
        this.isBenchmark = isBenchmark;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return new StarcraftBot( this );
    }

    public Map<String, ByteBuffer> getBinaryContent() {
        return files;
    }

    public String getName() {
        return name;
    }

    public String getRace() {
        return race;
    }

    public long getRandomHex() {
        return randomHex;
    }

    public boolean isBenchmarkBot() {
        return isBenchmark;
    }

    private void readObject( ObjectInputStream ois ) throws ClassNotFoundException, IOException {
        // default deserialization
        ois.defaultReadObject();

        //Transient field
        int filednumber = ois.readInt();

        while (filednumber > 0) {
            files = new HashMap<String, ByteBuffer>();
            final String name = (String) ois.readObject();
            final byte[] bytes = (byte[]) ois.readObject();
            files.put( name, ByteBuffer.wrap( bytes ) );
            filednumber--;
        }

    }

    public void setBinaryContent( Map<String, ByteBuffer> binaryContent ) {
        files = binaryContent;

    }

    @Override
    public String toString() {
        return "StarcraftBot [name=" + name + ", randomHex=" + randomHex + ", race=" + race + ", isBenchmark=" + isBenchmark + "]";
    }

    private void writeObject( ObjectOutputStream oos ) throws IOException {
        // default serialization 
        oos.defaultWriteObject();

        //Transient field
        oos.writeInt( files != null ? files.size() : 0 );
        if (files != null) {
            for (final Map.Entry<String, ByteBuffer> e : files.entrySet()) {
                oos.writeObject( e.getKey() );
                e.getValue().rewind();
                oos.writeObject( e.getValue().array() );
            }
        }
    }

}
